/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.ddtroops.web.actionbeans;

import cz.muni.fi.pa165.ddtroops.dto.RaceDTO;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.RaceService;
import java.util.List;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 *
 * @author Erik
 */
@UrlBinding("/races/{$event}/{race.id}")
public class RaceActionBean implements ActionBean {

    private ActionBeanContext context;

    @Override
    public void setContext(ActionBeanContext abc) {
        this.context = abc;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    @SpringBean(value="raceWebService")
    private RaceService service;

    @DefaultHandler
    public Resolution races(){
        return new ForwardResolution("/races.jsp");
    }

    public List<RaceDTO> getRaces(){
        return service.getAll();
    }

    private RaceDTO race;

        @ValidateNestedProperties(
        value= {
            @Validate(on={"add", "save"},field="name", required=true),
            @Validate(on={"add", "save"},field="description", required=true),
            @Validate(on={"add", "save"},field="strength", required=true, minvalue=-3, maxvalue=3),
            @Validate(on={"add", "save"},field="dexterity", required=true, minvalue=-3, maxvalue=3),
            @Validate(on={"add", "save"},field="constitution", required=true, minvalue=-3, maxvalue=3),
            @Validate(on={"add", "save"},field="intelligence", required=true, minvalue=-3, maxvalue=3),
            @Validate(on={"add", "save"},field="wisdom", required=true, minvalue=-3, maxvalue=3),
            @Validate(on={"add", "save"},field="charisma", required=true, minvalue=-3, maxvalue=3)
        }
    )
    public RaceDTO getRace() {
        return race;
    }

    public void setRace(RaceDTO race) {
        this.race = race;
    }

    public Resolution add(){
        service.create(race);
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution delete(){
        service.delete(race);
        return new RedirectResolution(this.getClass(), "all");
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save", "delete"})
    public void loadRaceFromDb(){
        String ids = context.getRequest().getParameter("race.getId()");
        if (ids == null) return;
        race = service.getById(Long.parseLong(ids));
    }

    public Resolution edit(){
        return new ForwardResolution("/racesEdit.jsp");
    }

    public Resolution save(){
        service.update(race);
        return new RedirectResolution (this.getClass(), "all");
    }
}
